import request from '../../utils/request.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videogroupList: [],
    navId: 0,
    videoList: [],
    videoId: '',
    videoUpdateTime: [],
    isTriggered:false
  },

  //跳转到搜索页面
  toSearch(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },

// 视频列表下拉刷新触发
  handleRefresherrefresh(){
    //再次发起请求 获取最新数据
  this.getVideoGroupList(this.data.navId);

  },
  // 视频播放完成触发
  handleEnd(event) {

    let {
      videoUpdateTime
    } = this.data;
    videoUpdateTime.splice(videoUpdateTime.findIndex(item => item.vid === event.currentTarget.id), 1);

    //更新视频变更时间数组
    this.setData({
      videoUpdateTime
    })
  },

  //当播放进度变化时触发
  handleUpdateTime(event) {

    let videoupdate = {
      vid: event.currentTarget.id,
      currentTime: event.detail.currentTime
    };

    let {
      videoUpdateTime
    } = this.data;
    let videoItem = videoUpdateTime.find(item => item.vid === videoupdate.vid);
    if (videoItem) {
      //之前有
      videoItem.currentTime = videoupdate.currentTime;
    } else {
      videoUpdateTime.push(videoupdate);
    }

    //更新视频变更时间数组
    this.setData({
      videoUpdateTime
    })

  },
  handlePlay(event) {
    let vid = event.currentTarget.id;

    // 解决多视频问题，只能一个视频播放
    // this.vid !==vid && this.videoContext && this.videoContext.stop();
    // this.vid = vid;
    this.setData({
      videoId: vid
    })

    this.videoContext = wx.createVideoContext(vid);

    //当播放视频使，判断之前有没有播放过，有的话，就跳过播放过的片段

    let {
      videoUpdateTime
    } = this.data;
    let videoItem = videoUpdateTime.find(item => item.vid === vid);

    if (videoItem) {

      this.videoContext.seek(videoItem.currentTime);
    }
    this.videoContext.play();

  },

  clickNav(event) {
    let navId = event.currentTarget.id;

    this.setData({
      navId: navId * 1,
      videoList: []
    })


    wx.showLoading({
      title: '正在加载中'
    })
    this.getVideoList(this.data.navId);


  },

  //获取视频列表数据
  async getVideoList(navId) {
    if (!navId) {
      return;
    }

    let res = await request('/video/group', {
      id: navId
    });
    wx.hideLoading();

    this.setData({
      videoList: res.datas,
      isTriggered: false
    })

  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getVideoGroupList();
  },

  async getVideoGroupList() {
    let res = await request('/video/group/list');

    this.setData({
      videogroupList: res.data.slice(0, 14),
      navId: res.data[0].id
    })

    this.getVideoList(this.data.navId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})